/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlykho;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Phuong Nam
 */
public class HDNhap extends javax.swing.JPanel implements ListSelectionListener{

    DefaultTableModel model;
    DefaultTableModel model1;
    

    public HDNhap(String User) {

        try {
            initComponents();
            
            Ketnoi a = new Ketnoi();
            Connection con;
            con = a.connect();
            String lh ="loaihang";
            loadCb(jc1,lh,2);
            model1 = (DefaultTableModel) jt2.getModel();
            
            PreparedStatement pst = con.prepareCall("Select * from dangnhap where [User] = ?");
            pst.setString(1, User);
            //System.out.println(User);
            ResultSet rs = pst.executeQuery();
            String mnv = null;
            while (rs.next()) {
                mnv = rs.getString(4);
            }
            //System.out.println(mnv);
            PreparedStatement pst1 = con.prepareCall("Select * from nhanvien where MaNV = ?");
            pst1.setString(1, mnv);
            rs = pst1.executeQuery();
            String TenNV = null;
            while (rs.next()) {
                TenNV = rs.getString(2);
            }
            System.out.println(TenNV);
            t2.setText(mnv);
            t6.setText(TenNV);
            t5.setText("0");
            t7.setText("0");
            t2.setEnabled(false);
            t6.setEnabled(false);
            t5.setEnabled(false);
            t7.setEnabled(false);
            t8.setEnabled(false);
            Them.setEnabled(false);
            xoa.setEnabled(false);
            xacnhan.setEnabled(false);

        } catch (SQLException ex) {
            Logger.getLogger(HDNhap.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void loadTable(String str)
    {
        try {
            model1 = (DefaultTableModel) jt2.getModel();
                int num=jt2.getRowCount();
                for (int i = num-1; i >= 0; i--) {
                    model1.removeRow(i);
                }
                Ketnoi a = new Ketnoi();
                Connection con = a.connect();
                PreparedStatement pst1 = con.prepareCall("Select * from loaihang where tenloaihh = ?");
                pst1.setString(1, str);
                ResultSet rs1= pst1.executeQuery();
                String str1= null;
                while(rs1.next())
                {
                    str1=rs1.getString(1);
                }
                System.out.println(str1);
                PreparedStatement pst = con.prepareCall("Select * from hanghoa where maloaihh = ?");
                pst.setString(1, str1);
                ResultSet rs = pst.executeQuery();
                Vector b = null;
                while(rs.next())
                {
                    b=new Vector();
                    b.add(rs.getString(1));
                    b.add(rs.getString(2));
                    model1.addRow(b);
                }
        } catch (SQLException ex) {
            Logger.getLogger(ThongTinHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void loadCb(JComboBox cb,String str,int index)
    {
        try {
            Ketnoi a = new Ketnoi();
            Connection con = a.connect();
            cb.removeAllItems();
            cb.addItem("Lựa chọn");
            String str1="Select * from "+str;
            System.out.println(str1);
            PreparedStatement pst = con.prepareCall(str1);
            ResultSet rs = pst.executeQuery();
            while(rs.next())
            {
                cb.addItem(rs.getString(index));  
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(ThongTinHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     public Vector getCb(String str,int index)
    {
        Vector v = new Vector();
        try {
            Ketnoi a = new Ketnoi();
            Connection con = a.connect();
            String str1="Select * from "+str;
            PreparedStatement pst = con.prepareCall(str1);
           ResultSet rs = pst.executeQuery();
            while(rs.next())
            {
                v.add(rs.getString(index));  
            }
            String st = null;
        } catch (SQLException ex) {
            Logger.getLogger(ThongTinHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
        return v;
    }
    /**
     * Creates new form HDNhap
     */
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel9 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        t1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        t3 = new javax.swing.JTextField();
        t2 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jt1 = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        t4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        Them = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        t8 = new javax.swing.JTextField();
        xoa = new javax.swing.JButton();
        xacnhan = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        t6 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        t7 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        t5 = new javax.swing.JTextField();
        jc1 = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jt2 = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Phiếu Nhập Kho");
        jLabel9.setToolTipText("");

        jLabel1.setText("Mã phiếu nhập :");

        t1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Mã nhân viên :");

        jLabel3.setText("Ngày nhập :");

        t3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t3ActionPerformed(evt);
            }
        });

        t2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        t2.setBorder(null);
        t2.setDisabledTextColor(new java.awt.Color(255, 0, 51));
        t2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t2ActionPerformed(evt);
            }
        });

        jt1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã SP", "Tên SP", "Số lượng", "Giá Nhập", "Thành Tiền"
            }
        ));
        jScrollPane1.setViewportView(jt1);

        jLabel4.setText("Mã sản phẩm :");

        jButton1.setText("Kiểm Tra");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        Them.setText("Thêm");
        Them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ThemActionPerformed(evt);
            }
        });

        jLabel5.setText("Số lượng");

        xoa.setText("Xóa");
        xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xoaActionPerformed(evt);
            }
        });

        xacnhan.setText("Xác Nhận");
        xacnhan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xacnhanActionPerformed(evt);
            }
        });

        jLabel6.setText("Tên nhân viên :");

        t6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        t6.setBorder(null);
        t6.setDisabledTextColor(new java.awt.Color(255, 0, 51));

        jLabel7.setText("Số mặt hàng :");

        t7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        t7.setBorder(null);
        t7.setDisabledTextColor(new java.awt.Color(255, 0, 0));
        t7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t7ActionPerformed(evt);
            }
        });

        jLabel8.setText("Giá trị đơn hàng :");

        t5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        t5.setBorder(null);
        t5.setDisabledTextColor(new java.awt.Color(255, 0, 0));

        jc1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jc1ActionPerformed(evt);
            }
        });

        jt2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Hàng", "Tên Hàng"
            }
        ));
        jScrollPane2.setViewportView(jt2);

        jLabel10.setText("Loại hàng :");

        jButton2.setText("Lựa chọn");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(t3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                                    .addComponent(t1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                                    .addComponent(t2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                                    .addComponent(t4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                                    .addComponent(jc1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(75, 75, 75)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(9, 9, 9))
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 45, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(t5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(t6)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(t8, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(Them))
                            .addComponent(t7)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(242, 242, 242)
                        .addComponent(xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(xacnhan))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(t5, javax.swing.GroupLayout.DEFAULT_SIZE, 21, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1)
                        .addComponent(jLabel8)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(t6, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel6)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(t3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(t7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(t4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addComponent(jLabel5)
                    .addComponent(t8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Them))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jc1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(xoa)
                    .addComponent(xacnhan))
                .addGap(40, 40, 40))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void t1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_t1ActionPerformed

    private void t3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_t3ActionPerformed

    private void t2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_t2ActionPerformed

    private void xoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xoaActionPerformed
        // TODO add your handling code here:
        int cot = 0;
        cot = jt1.getSelectedRow();
        if (cot < 0) {
            JOptionPane.showMessageDialog(this, "Hãy lựa chọn hàng cần xóa.");
        }
        if (cot >= 0) {
            String str = jt1.getValueAt(cot, 4).toString();
            int giatien = 0;
            giatien = Integer.parseInt(str);
            System.out.println(giatien);
            int tong = Integer.parseInt(t5.getText());
            tong = tong - giatien;
            t5.setText(String.valueOf(tong));
            model.removeRow(cot);

            int i = Integer.parseInt(t7.getText());
            i = i - 1;
            t7.setText(String.valueOf(i));
        }

    }//GEN-LAST:event_xoaActionPerformed

    private void t7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t7ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_t7ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Ketnoi a = new Ketnoi();
        Connection con = a.connect();
        try {
            // TODO add your handling code here:
            String msp = null;
            msp = t4.getText().toUpperCase();
            PreparedStatement pst = con.prepareCall("Select * from hanghoa where MaHH = ?");
            pst.setString(1, msp);
            ResultSet rs = pst.executeQuery();
            int sl = -1;
            while (rs.next()) {
                sl = rs.getInt(5);
            }
            if (sl == -1) {
                JOptionPane.showMessageDialog(this, "Không có mã sản phẩm này.");
            }
            if (sl == 0) {
                JOptionPane.showMessageDialog(this, "Sản phẩm này đã hết hàng.\nHãy nhập thêm số sản phẩm.");
                t8.setEnabled(true);
                Them.setEnabled(true);
                t8.requestFocus();
            }
            if (sl > 0) {
                JOptionPane.showMessageDialog(this, "Sản phẩm này còn : " + sl + " sản phẩm.");
                t8.setEnabled(true);
                Them.setEnabled(true);
                t8.requestFocus();
            }
        } catch (SQLException ex) {
            Logger.getLogger(HDNhap.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void ThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ThemActionPerformed
        try {
            // TODO add your handling code here:
            boolean flag = true;
            String msp = null;
            msp = t4.getText().toUpperCase();
            model = (DefaultTableModel) jt1.getModel();
            Ketnoi a = new Ketnoi();
            Connection con = a.connect();
            if (t4.getText().length() == 0) {
                JOptionPane.showMessageDialog(this, "Hãy nhập vào mã sản phẩm.");
                t4.requestFocus();
                flag = false;
            }
            //De phan nay lam form Xuat hang
            int test = 0;
            PreparedStatement pst1 = con.prepareCall("Select * from hanghoa where MaHH = ?");
            pst1.setString(1, msp);
            ResultSet rs1 = pst1.executeQuery();
            while (rs1.next()) {
                test++;
            }
            if (test == 0 && flag == true) {
                JOptionPane.showMessageDialog(this, "Mã sản phẩm không chính xác vui lòng kiểm tra lại.");
                t4.requestFocus();
                t4.setText("");
                flag = false;
            }
//            int soluongnhap = Integer.parseInt(t8.getText());
//            if(soluongnhap > soluongcon)
//            {
//                JOptionPane.showMessageDialog(this,"S");
//                t8.requestFocus();
//                flag = false;
//            }

            if (t8.getText().length() == 0 && flag == true) {
                JOptionPane.showMessageDialog(this, "Hãy nhập vào số lượng sản phẩm.");

                t8.requestFocus();

                flag = false;
            }

            if (flag == true) {
                xoa.setEnabled(true);
                xacnhan.setEnabled(true);
                PreparedStatement pst = con.prepareCall("Select * from hanghoa where MaHH = ?");
                pst.setString(1, msp);
                ResultSet rs = pst.executeQuery();
                Vector b = new Vector();
                int tong = Integer.parseInt(t5.getText());
                int s = 0;
                int a1, a2;
                while (rs.next()) {
                    a1 = rs.getInt(7);
                    a2 = Integer.parseInt(t8.getText());
                    b.add(rs.getString(1));
                    b.add(rs.getString(2));
                    b.add(t8.getText());
                    b.add(rs.getInt(7));
                    s = a1 * a2;
                    b.add(s);
                    model.addRow(b);
                    tong = tong + s;
                }
                t5.setText(String.valueOf(tong));
                int socot = jt1.getRowCount();
                System.out.println(socot);
                t7.setText(String.valueOf(socot));
                t4.setText("");
                t8.setText("");
            }
        } catch (SQLException ex) {
            Logger.getLogger(HDNhap.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_ThemActionPerformed

    private void xacnhanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xacnhanActionPerformed
        try {
            // TODO add your handling code here:
            Ketnoi a = new Ketnoi();
            Connection con = a.connect();
            int flag = 1;
            if (t1.getText().length() == 0) {
                flag = 2;
            }
            if (t3.getText().length() == 0 && flag == 1) {

                flag = 3;
            }
            int row = 0;
            row = jt1.getRowCount();
            int col = 0;
            col = jt1.getColumnCount();
            if (row == 0 && flag == 1) {

                flag = 4;
            }
            if (flag == 2) {
                JOptionPane.showMessageDialog(this, "Bạn chưa nhập mã phiếu hàng.");
                t1.requestFocus();
                flag = 2;
            }
            if (flag == 3) {
                JOptionPane.showMessageDialog(this, "Bạn chưa nhập ngày nhập.");
                t3.requestFocus();
                flag = 3;
            }
            if (flag == 4) {
                JOptionPane.showMessageDialog(this, "Phiếu hàng không có mặt hàng nào \n Hãy thêm mặt hàng vào");
                t4.requestFocus();
                flag = 4;
            }
            if (flag == 1) {
                int close = JOptionPane.showConfirmDialog(this, "Bạn có muốn xác nhân.", "Notice", 2);
                if (close == 1) {
                    flag = 0;
                }
                if (close == 0) {
                    flag = 5;
                }
            }

            if (flag == 5) {
                int gianhap = 0;
                int soluong = 0;
                int tong = 0;
                String str1 = null;
                String str2 = null;
                int i;
                PreparedStatement pstcheck1 = con.prepareCall("alter table mathangnhap nocheck constraint FK_MatHangNhap_PhieuNhap");
                pstcheck1.executeUpdate();
                for (i = 0; i < row; i++) {

                    str1 = t1.getText();
                    System.out.println(str1);
                    str2 = jt1.getValueAt(i, 0).toString();
                    gianhap = Integer.parseInt(jt1.getValueAt(i, 3).toString());
                    soluong = Integer.parseInt(jt1.getValueAt(i, 2).toString());
                    tong = Integer.parseInt(jt1.getValueAt(i, 4).toString());
                    PreparedStatement pst1 = con.prepareCall("Insert into MatHangNhap values (?,?,?,?,?)");
                    pst1.setString(1, str1);
                    pst1.setString(2, str2);
                    pst1.setInt(3, gianhap);
                    pst1.setInt(4, soluong);
                    pst1.setInt(5, tong);
                    pst1.executeUpdate();
                    PreparedStatement pst2 = con.prepareCall("Select * from HangHoa where MaHH = ?");
                    pst2.setString(1, str2);
                    ResultSet rs = pst2.executeQuery();
                    int soluonghientai = 0;
                    while (rs.next()) {
                        soluonghientai = rs.getInt(5);
                    }
                    System.out.println(soluonghientai);
                    System.out.println(soluong);
                    soluonghientai = soluonghientai + soluong;
                    PreparedStatement pst3 = con.prepareCall("Update  HangHoa set soluong = ? where MaHH = ?");
                    pst3.setInt(1, soluonghientai);
                    pst3.setString(2, str2);
                    pst3.executeUpdate();
                }
                PreparedStatement pst = con.prepareCall("Insert into PhieuNhap values (?,?,?,?,?)");
                pst.setString(1, t1.getText());
                pst.setString(2, t2.getText());
                pst.setString(3, t3.getText());
                pst.setInt(4, Integer.parseInt(t7.getText()));
                pst.setInt(5, Integer.parseInt(t5.getText()));
                pst.executeUpdate();
                PreparedStatement pstcheck2 = con.prepareCall("alter table mathangnhap check constraint FK_MatHangNhap_PhieuNhap");
                pstcheck2.executeUpdate();
                JOptionPane.showMessageDialog(this, "Xác nhận thành công.");
                t1.setText("");
                t3.setText("");
                t4.setText("");
                t5.setText("0");
                t7.setText("0");
                t8.setText("");
                t8.setEnabled(false);
                Them.setEnabled(false);
                xoa.setEnabled(false);
                xacnhan.setEnabled(false);
                int num = jt1.getRowCount();
                for (int j = num - 1; j >= 0; j--) {
                    model.removeRow(j);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(HDNhap.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_xacnhanActionPerformed

    private void jc1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jc1ActionPerformed
        // TODO add your handling code here:

        String lh="loaihang";
        String lhang=null;
        Vector t=null;
        //t = new Vector();
        t=getCb(lh,2);
        int luachon=0;
        luachon=jc1.getSelectedIndex();
        if(luachon>0)
        {
        
        lhang=t.get(luachon-1).toString();
        //System.out.println(luachon+lhang);
        loadTable(lhang);
        }
    }//GEN-LAST:event_jc1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        int num1=jt2.getSelectedRow();
        String str5=null;
        
        if(num1>=0)
        {
            str5 = jt2.getValueAt(num1,0).toString();
            t4.setText(str5);
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    private void load()
    {
        String lh="loaihang";
        Vector t=null;
        t=getCb(lh,2);
        int luachon=0;
        luachon=jc1.getSelectedIndex();
        if(luachon>0)
        {
        String lhang=null;
        lhang=t.get(luachon-1).toString();
        //System.out.println(luachon+lhang);
        loadTable(lhang);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Them;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JComboBox jc1;
    private javax.swing.JTable jt1;
    private javax.swing.JTable jt2;
    private javax.swing.JTextField t1;
    private javax.swing.JTextField t2;
    private javax.swing.JTextField t3;
    private javax.swing.JTextField t4;
    private javax.swing.JTextField t5;
    private javax.swing.JTextField t6;
    private javax.swing.JTextField t7;
    private javax.swing.JTextField t8;
    private javax.swing.JButton xacnhan;
    private javax.swing.JButton xoa;
    // End of variables declaration//GEN-END:variables

    @Override
    public void valueChanged(ListSelectionEvent e) {
        
        int num1=jt2.getSelectedRow();
        String str5=null;
        str5 = jt2.getValueAt(num1,0).toString();
        //jt2.getSelectionModel().removeListSelectionListener(this);

    }
}
